starter.controller('bookingPassengerController', function($rootScope,$state,$ionicPopup,$timeout,$scope,Page, localStore,$location,$ionicHistory,sessionStore) {
		$scope.line = $state.params.line || sessionStore.getObject('booking.line') || {};
		$scope.order = $state.params.order  || sessionStore.getObject('booking.order') || {};
		
		if(!$scope.order || !$scope.line || !$scope.line.stops 
				|| !$scope.order.plan || !$scope.order.plan.planId
				|| !$scope.order.rideDates || $scope.order.rideDates.length == 0){
			return false;
		}
		
		$scope.order.passengers = [];
		
		$scope.onStops = []; 
		$scope.offStops = []; 
		$($scope.line.stops).each(function(idx, stop){
			if(stop.stopType == '1'){
				$scope.onStops.push({
					id: stop.stopId,
					value: stop.stopName
				});
			}else if(stop.stopType == '2'){
				$scope.offStops.push({
					id: stop.stopId,
					value: stop.stopName
				});
			}
		});
		$scope.addPassenger = function(user){
			user = user || {};
			$scope.order.passengers.push({
				name : user.realName || '',
				phone : user.phone || '',
				onStopId : $scope.onStops.length > 0 ? $scope.onStops[0].id : '',
				offStopId : $scope.offStops.length > 0 ? $scope.offStops[0].id : ''
			});
		};
		$scope.addPassenger($rootScope.user);//初始添加一位乘客
		
		$scope.removePassenger = function(passenger,idx){
			if(!$scope.order.passengers || $scope.order.passengers.length <= 1){
				Page.alert("至少保留一位乘车!");
				return;
			}
			Page.confirm("确认取消该名乘客?",function(){
				$scope.order.passengers.splice(idx,1);	
			});
		};
		
		$scope.selectOnStop = function(passenger){
			var addressSelect = new IosSelect(1, 
	            [$scope.onStops],
	            {
	                container: '.optionsSelectAddressContainer',
	                title: '选择上车地点',
	                itemHeight: 40,
	                itemShowCount: 5,
	                oneLevelId:passenger.onStopId,
	                callback: function (selectOneObj) {
						$scope.$apply(function () {
					     	passenger.onStopId = selectOneObj.id;
					    });
	                }
	        });
		};
		
		$scope.selectOffStop = function(passenger){
			var addressSelect = new IosSelect(1, 
	            [$scope.offStops],
	            {
	                container: '.optionsSelectAddressContainer',
	                title: '选择上车地点',
	                itemHeight: 40,
	                itemShowCount: 5,
	                oneLevelId:passenger.offStopId,
	                callback: function (selectOneObj) {
						$scope.$apply(function () {
							passenger.offStopId = selectOneObj.id;
					    });
	                }
	        });
		};
		
		$scope.next = function(){
			if(!$scope.order.passengers || $scope.order.passengers.length == 0){
				Page.alert("请至少填写一位乘车信息!");
				return;
			}
			var valid = true;
			var phoneRegex = /^1[3|4|5|7|8]\d{9}$/;

			$($scope.order.passengers).each(function(idx, passenger){
				if($scope.line.lineType == '1' && passenger.name == ''){//定制公交
					Page.alert("乘客姓名不能为空!");
					valid = false;
					return false;
				}else if($scope.line.lineType == '2' && passenger.name == ''){//职工专线
					Page.alert("职工编号不能为空!");
					valid = false;
					return false;
				}else if(passenger.name == ''){
					Page.alert("乘客不能为空!");
					valid = false;
					return false;
				}

				if(passenger.phone == ''){
					Page.alert("手机号码不能为空!");
					valid = false;
					return false;
				}else if(!phoneRegex.test(passenger.phone)){
					Page.alert("手机号码格式不正确!");
					valid = false;
					return false;
				}
				if(passenger.onStopId == ''){
					Page.alert("上车地点不能为空!");
					valid = false;
					return false;
				}
			});


			if(isRepeatPhone($scope.order.passengers)) {
				Page.notice("乘客的电话号码重复!");
				return;
			}

			if(valid){
				sessionStore.setObject('booking.order', $scope.order);
				$state.go('booking_confirm', {line: $scope.line, order: $scope.order},{reload:true});
			}
		};

		function isRepeatPhone(passengers){
			var hash = {};
			for(var i in passengers) {
				if(hash[passengers[i].phone])
					return true;
				hash[passengers[i].phone] = true;
			}
			return false;
		}

});